package model;

import controller.DatabaseController;
import controller.ItemController;
import controller.MessageController;
import controller.RoomController;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;


public class Player extends Figure {

    // 玩家属性

    private String id; // 玩家唯一标识符

    protected int experience; // 玩家经验值

    private final HashMap<String,Item> bag=new HashMap<>();//背包

    // 无参构造函数，初始化默认值
    public Player() {
        super();
    }

    // 全参构造函数，用于创建带有初始值的玩家对象
    public Player(int experience, int con, int dex, int str, int wis, int hp, int max_hp, int nl, int max_nl, int jl, int max_jl, String id, String username, String location,int type) {
        super(con, dex, str, wis, hp, max_hp, nl, max_nl, jl, max_jl, username, location,type);
        this.id = id;
        this.experience = experience;
    }

    // 玩家移动方法
    public void move(CommonContent.DIRECTION direction) throws IOException {
        Room from=RoomController.getRoom(getLocation());
        Room to=from.getDes(direction);
        if(to!=null) {
            from.exit(this,direction);
            to.enter(this,RoomController.getop(direction));
            this.setLocation(to.getRoomId());
        } else{
            MessageController.showToPlayer(this,"您选择的方向没有房间");
        }
    }

    // 玩家查看方法
    public void look(String something) throws IOException {
        if (something.equals("")) {
            MessageController.showToPlayer(this,"\n\n\n"+RoomController.getRoom(this.getLocation()).getRoomLooking());
        } else if (something.equals("bag")) {
            showBag();
        } else if (something.equals("self")) {
            MessageController.showToPlayer(this,"\n\n\n"+getDescription());
        } else {
            Room room=RoomController.getRoom(this.getLocation());
            if(room.have(something)) {
                Item i=room.getItem(something);
                MessageController.showToPlayer(this,"\n\n\n"+i.getDesc());
            } else if (bag.containsKey(something)) {
                Item i=bag.get(something);
                MessageController.showToPlayer(this,"\n\n\n"+i.getDesc());
            } else if(room.npcExist(something)) {
                Npc n=room.getNpc(something);
                MessageController.showToPlayer(this,"\n\n\n"+n.getDescription());
            }else if(room.monsterExists(something)) {
                Monster m=room.getMonster(something);
                MessageController.showToPlayer(this,"\n\n\n"+m.getDescription());
            }else if(room.playerExist(something)) {
                Player p=room.getPlayer(something);
                MessageController.showToPlayer(this,"\n\n\n"+p.getDescription());
            }else{
                MessageController.showToPlayer(this,"周围不存在这个物品或人物。");
            }
        }
    }

    private String getDescription() {
        String tmp="";
        tmp+="名字:"+getName()+"\n";
        tmp+="con:"+getCon()+"\n";
        tmp+="dex:"+getDex()+"\n";
        tmp+="str:"+getStr()+"\n";
        tmp+="wis:"+getWis()+"\n";
        tmp+="hp:"+getHp()+"/"+getMax_hp()+"\n";
        tmp+="nl:"+getNl()+"/"+getMax_nl()+"\n";
        tmp+="jl:"+getJl()+"/"+getMax_jl()+"\n";
        return tmp;
    }

    private void showBag() throws IOException {
        String tmp="";
        tmp+="\n\n\n"+"背包:\n";
        for(Item i:bag.values()) {
            tmp+=i.getName()+"x"+i.getNum()+"\n";
        }
        MessageController.showToPlayer(this,tmp);
    }

    public Item getItem(String something) {
        return bag.get(something);
    }

    public void get(String object) throws IOException {//获得一个物体
        Room r=RoomController.getRoom(getLocation());
        if(!r.have(object)){
            MessageController.showToPlayer(this,"当前区域没有此物品。");
            return;
        }
        Item item=r.getItem(object);
        if(bag.containsKey(object)){
            Item had=bag.get(object);
            if(had.check(item.getNum())){
                had.setNum(had.getNum()+item.getNum());
                r.itemgot(item);
                MessageController.showToPlayer(this,"拾取成功。");
            } else{
                int num=item.getMaxnum();
                item.setNum(item.getNum()+had.getNum()-num);
                had.setNum(num);//拾取到最大值
                MessageController.showToPlayer(this,"您拥有此物品的数量已达上限。");
            }
            }else {
            if(item.check(0)){
                bag.put(object,item);
                r.itemgot(item);
                MessageController.showToPlayer(this,"拾取成功。");
            }else {
                Item got=ItemController.getItem(object);
                got.setNum(got.getMaxnum());
                bag.put(object,got);
                item.setNum(item.getNum()-item.getMaxnum());
                MessageController.showToPlayer(this,"已拾取至上限。");
            }

            }
    }

    // 玩家退出游戏方法
    public void quit() throws SQLException, IOException {
        DatabaseController.updatePlayer(this);
        MessageController.showToPlayer(this,"您已退出");
        MessageController.removePlayerChannels(this.getId());
    }


    // 获取玩家ID
    public String getId() {

        return id;
    }

    // 设置玩家ID（通常不建议修改，除非有特殊需求）
    public void setId(String id) {
        this.id = id;
    }

    public int getExperience() {
        return experience;
    }

    public void setExperience(int experience) {
        this.experience = experience;
    }

    public String getdesc(){
        String tmp="";
        tmp+="名字:"+getName()+"\n";
        tmp+="con:"+getCon()+"\n";
        tmp+="dex:"+getDex()+"\n";
        tmp+="str:"+getStr()+"\n";
        tmp+="wis:"+getWis()+"\n";
        tmp+="hp:"+getHp()+"/"+getMax_hp()+"\n";
        tmp+="nl:"+getNl()+"/"+getMax_nl()+"\n";
        tmp+="jl:"+getJl()+"/"+getMax_jl()+"\n";
        tmp+="exp:"+getExperience()+"\n";
        return tmp;
    }

    public HashMap<String,Item> getBag() {
        return bag;
    }

    public void throwthing(String thing) throws IOException {
        Item i=bag.get(thing);
        if(i==null){
            MessageController.showToPlayer(this,"您没有此物品。");
        }else {
            bag.remove(thing);
            i.setLocation(getLocation());
            ItemController.itemInit(i);
            MessageController.showToPlayer(this,"丢弃物品成功。");
        }

    }
}
